﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Weight.Base;
using Weight.Base.Utility;
using Weight.Dal.CarManage;
using Weight.Model;

namespace Weight.Bll.CarManage
{
    public class BllCarInfo
    {
        private DalCarInfo Dal = new DalCarInfo();
        /// <summary>
        /// 查询车号列表
        /// </summary>
        /// <param name="plateNum">车号</param>
        /// <returns></returns>
        public IEnumerable<CarInfoEntity>Query(string plateNum)
        {
           return Dal.Query(plateNum);
        }

        /// <summary>
        /// 根据Id获取车辆信息
        /// </summary>
        /// <param name="id">编号</param>
        /// <returns></returns>
        public CarInfoEntity GetById(string id)
        {
            if(string.IsNullOrEmpty(id))
            {
                return null;
            }
            else
            {
                return Dal.GetById(id);
            }
        }

        /// <summary>
        /// 根据Id获取车辆信息
        /// </summary>
        /// <param name="id">编号</param>
        /// <returns></returns>
        public Dictionary<string,string> GetMapById(string id)
        {
            Dictionary<string, string> dic = new Dictionary<string, string>();
            if (string.IsNullOrEmpty(id))
            {
                return null;
            }
            else
            {
                var data =  Dal.GetById(id);
                dic = ReflectHelper.ParseEntityToMap(data);
            }
            return dic;
        }

      

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public Tuple<bool,string> Add(CarInfoEntity entity)
        {
           var item =  Dal.GetByCarNo(entity.PlateNumber);
            if(item != null)
            {
                return new Tuple<bool, string>(false, "该车号已经存在，不能重复添加!");
            }
            else
            {
                bool b =  Dal.Add(entity);
                if(b)
                {
                    return new Tuple<bool, string>(true,"添加成功");
                }
                else
                {
                    return new Tuple<bool, string>(true, "添加失败");
                }
            }
            
        }


        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public Tuple<bool, string> Update(CarInfoEntity entity)
        {
            var item = Dal.GetByCarNo(entity.PlateNumber);
            if (item != null)
            {
                if(entity.Id != item.Id)
                {
                    return new Tuple<bool, string>(false, "该车号已经存在，不能重复添加!");
                }
            }
            bool b = Dal.Update(entity);
            if (b)
            {
                return new Tuple<bool, string>(true, "修改成功");
            }
            else
            {
                return new Tuple<bool, string>(false, "修改失败");
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool Delete(CarInfoEntity entity)
        {
            return Dal.Delete(entity);
        }
    }
}
